﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

using Uber.Storage;
using Uber.Messages;
using Uber.HabboHotel.GameClients;
using Uber.HabboHotel.Pathfinding;
using Uber.HabboHotel.Rooms;
using System.Threading;
using System.Threading.Tasks;

namespace Uber.HabboHotel.Items.Interactors
{
    class InteractorFirework : FurniInteractor
    {
        public override bool OnPlace(GameClient Session, RoomItem Item)
        {
            if (Item.FireWorkCount > 0)
            {
                Item.ExtraData = "1";

                if (Session != null)
                {
                    Item.UpdateState(false, true);
                }
            }
            else if (Item.FireWorkCount <= 0)
            {
                Item.ExtraData = "0";

                if (Session != null)
                {
                    Item.UpdateState(false, true);
                }
            }
            return true;
        }

        public override void OnRemove(GameClient Session, RoomItem Item) { }

        public override void OnTrigger(GameClient Session, RoomItem Item, int Request, bool UserHasRights)
        {
            RoomUser User = Item.GetRoom().GetRoomUserByHabbo(Session.GetHabbo().Id);

            if (User == null)
            {
                return;
            }

            if (Request == 0 && Item.FireWorkCount > 0 && Item.ExtraData == "1")
            {
                Item.ExtraData = "2";
                Item.UpdateState(false, true);

                if (Item.FireWorkCount > 0)
                {
                    Task T = null;
                    T = new Task(delegate() { Action(Item, T, "1"); });
                    T.Start();
                }
                else
                {
                    Task T = null;
                    T = new Task(delegate() { Action(Item, T, "0"); });
                    T.Start();
                }
            }
            else if (Request == 1)
            {
                ServerMessage PixelMessage = new ServerMessage(629);
                PixelMessage.AppendUInt(Item.Id);
                PixelMessage.AppendInt32(Item.FireWorkCount);
                PixelMessage.AppendBoolean(false);
                PixelMessage.AppendInt32(100); // pixels :D
                PixelMessage.AppendBoolean(false);
                PixelMessage.AppendInt32(10); // 10 fireworks :D
                Session.SendMessage(PixelMessage);
            }
            else if (Request == 2)
            {
                int Count = 10; if (Session.GetHabbo().IsUltraVip) { Count = 100; }

                Item.FireWorkCount += Count;

                Session.GetHabbo().FireworkCount += Count;
                Session.GetHabbo().ActivityPoints -= 10;
                Session.GetHabbo().UpdateActivityPointsBalance(true);

                using (DatabaseClient dbClient = UberEnvironment.GetDatabase().GetClient())
                {
                    dbClient.AddParamWithValue("itemid", Item.Id);
                    dbClient.AddParamWithValue("sessionid", Session.GetHabbo().Id);
                    dbClient.ExecuteQuery("UPDATE items SET fw_count = fw_count + '" + Count + "' WHERE id = @itemid LIMIT 1");
                    dbClient.ExecuteQuery("UPDATE users SET fireworks = fireworks + '" + Count + "' WHERE id = @sessionid LIMIT 1");
                }

                ServerMessage PixelMessage = new ServerMessage(629);
                PixelMessage.AppendUInt(Item.Id);
                PixelMessage.AppendInt32(Item.FireWorkCount);
                PixelMessage.AppendBoolean(false);
                PixelMessage.AppendInt32(100); // pixels :D
                PixelMessage.AppendBoolean(false);
                PixelMessage.AppendInt32(10); // 10 fireworks :D
                Session.SendMessage(PixelMessage);

                if (Item.ExtraData == "0")
                {
                    Item.ExtraData = "1";
                    Item.UpdateState(false, true);
                }

                Session.GetHabbo().CheckFireworkAchievements();
            }
        }

        public void Action(RoomItem Item, Task Task, string ExtraData)
        {
            if (ExtraData == "1")
            {
                Task.Wait(3000);
                Item.FireWorkCount--;

                using (DatabaseClient dbClient = UberEnvironment.GetDatabase().GetClient())
                {
                    dbClient.AddParamWithValue("itemid", Item.Id);
                    dbClient.ExecuteQuery("UPDATE items SET fw_count = fw_count - 1 WHERE id = @itemid LIMIT 1");
                }
            }

            if (Item.FireWorkCount == 0)
            {
                ExtraData = "0";
            }

            Item.ExtraData = ExtraData;
            Item.UpdateState(false, true);

            Task.Wait();
            Task.Dispose();
        }
    }
}
